package com.lanou.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanou.bean.CompanyBean;
import com.lanou.bean.DynamicScore;
import com.lanou.bean.UserComments;
import com.lanou.mapper.CompanyMapper;
import com.lanou.mapper.UserCommentMapper;
import com.lanou.service.CompanyService;
import com.lanou.utils.AjaxResult;
import com.lanou.utils.StrUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by dllo on 18/3/12.
 */
@Service
public class CompanyServiceImpl implements CompanyService {

    @Resource
    private CompanyMapper companyMapper;
    @Resource
    private UserCommentMapper userCommentMapper;

    /**
     * 郭中宏
     * @param city 城市名
     * @param pageNo 当前页数
     * @param pageSize 每页显示多少条数据
     * @return 返回该城市所有的公司
     * 该接口实现 输入城市名、当前页数、每页显示多少条数据  返回 对应页数的所有城市和当前页数 总页数
     */
    @Override
    public PageInfo<CompanyBean> cityCompany(String city,Integer pageNo,Integer pageSize) {
        pageNo=pageNo==null?1:pageNo;
        pageSize=pageSize==null?10:pageSize;
        PageHelper.startPage(pageNo,pageSize);
        String cityStr="%"+city+"%";
        List<CompanyBean> list=companyMapper.cityCompany(cityStr);
        PageInfo<CompanyBean> pageInfo=new PageInfo<CompanyBean>(list);
        return pageInfo;
    }

    /**
     * zfy
     * 实现查询公司下订单信息的Mapper接口
     * @param cid
     * @return
     */
    @Override
    public AjaxResult getAllOrderInfoByCid(Integer cid) {
        AjaxResult result = new AjaxResult();
        result.setCode("200");
        result.setMsg("获得订单信息");
        result.setData(companyMapper.getAllOrderInfoByCid(cid));
        return result;
    }




    /**
     * 郭中宏
     * @param city 城市名
     * @param area 服务区域名
     * @param type 装修类型
     * @param price 承接价格
     * @param pageNo 当前页数
     * @param pageSize 每页显示多少数据
     * @param address 注册区域
     * @param style 专长风格
     * @return 返回对应的公司信息集合
     */
    @Override
    public PageInfo<CompanyBean> areaTypePriceCompany(String city,String area, String type, String price,String address,String style, Integer pageNo, Integer pageSize,String sortStr) {
        pageNo=pageNo==null?1:pageNo;
        pageSize=pageSize==null?10:pageSize;
        PageHelper.startPage(pageNo,pageSize);
        city="%"+city+"%";
        if (area!=null){
            area="%"+area+"%";
        }
        if (type!=null){
            type="%"+type+"%";
        }
        if (price!=null){
            price="%"+price+"%";
        }
        if (address!=null){
            address="%"+address+"%";
        }
        if (style!=null){
            style="%"+style+"%";
        }
        int sort=1;
        if (sortStr!=null){
            switch (sortStr){
                case "热门":
                    sort=1;
                    break;
                case "综合":
                    sort=2;
                    break;
                case "口碑":
                    sort=3;
                    break;
            }
        }
        System.out.println(sort);
        List<CompanyBean> list=companyMapper.areaTypePriceCompany(city,area,type,price,address,style,sort);
        PageInfo<CompanyBean> pageInfo=new PageInfo<CompanyBean>(list);
        return pageInfo;
    }

    /**
     * @param company_id   公司id
     * @param state        阶段状态
     * @param commentLevel 被评等级
     * @param pageNum      当前页数
     * @param pageSize     每页多少条
     * @return 返回对该公司的所有评论
     * 开发人:赵帅
     */

    @Override
    public PageInfo<UserComments> getAllComments(Integer company_id, String state, String commentLevel, Integer pageNum, Integer pageSize) {
        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 5 : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        state = StrUtils.charSetStr(state);
        commentLevel = StrUtils.charSetStr(commentLevel);
        List<UserComments> userCommentsList = userCommentMapper.getAllComments(company_id, commentLevel, state);
        PageInfo<UserComments> pageInfo = new PageInfo<UserComments>(userCommentsList);
        return pageInfo;
    }

    /**
     *
     * @param company_id 公司id
     * @return 动态评分数据
     * 开发人:赵帅
     */
    @Override
    public AjaxResult getAVGScore(Integer company_id) {
        DynamicScore dynamicScore = userCommentMapper.getAVGScore(company_id);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setCode("200");
        ajaxResult.setMsg("动态评分");
        ajaxResult.setData(dynamicScore);
        return ajaxResult;


    }


}
